package com.itheima.tcp.socket.socketDemo1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServiceDemo1 {
    public static void main(String[] args) throws IOException {
        //创建服务器端的socket对象
        ServerSocket ss = new ServerSocket(10086);

        //监听客户端连接
        Socket accept = ss.accept();    // 阻塞方法

        //获得输入流对象
        /*InputStream is = accept.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String res = br.readLine();
        System.out.println(accept.getLocalAddress() + ":" + accept.getLocalPort() + "\n" + res);*/
        InputStream br = accept.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = br.read(bytes)) != -1){   // Socket中的read() 也有阻塞效果
            System.out.println(accept.getLocalAddress() + ":" + accept.getLocalPort() + "\n" + new String(bytes,0,len));
        }


        //释放资源
        br.close();
        ss.close();
    }
}
